package com.hoshiicloud.withdraw.util.tanhaogong;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description: 日期工具类
 * @Author: LvYue
 * @Date: 2019-04-24
 */
@SuppressWarnings("ALL")
public class DateUtils {

	private DateUtils() {
	}

	private static SimpleDateFormat sf;


	/**
	 * 根据指定格式获取当前日期字符串
	 *
	 * @param pattern
	 * @return
	 */
	public static String getTodayStr(String pattern) {

		sf = new SimpleDateFormat(pattern);

		return sf.format(new Date());
	}

	/**
	 * 根据指定格式，格式化给定的日期获取当前日期字符串
	 *
	 * @param pattern
	 * @param date
	 * @return
	 */
	public static String formatDate(String pattern, Date date) {

		sf = new SimpleDateFormat(pattern);

		String format = null;

		if (date != null) {
			format = sf.format(date);
		}

		return format;
	}

	/**
	 * 根据指定格式，将字符串日期转换为date对象
	 *
	 * @param pattern
	 * @param dateStr
	 * @return
	 */
	public static Date formatDate(String pattern, String dateStr) {
		sf = new SimpleDateFormat(pattern);

		try {
			Date parse = sf.parse(dateStr);

			return parse;
		} catch (ParseException e) {
			e.printStackTrace();

			return null;
		}
	}

	/**
	 * 获取当前时间的年
	 *
	 * @return
	 */
	public static String getYears() {

		sf = new SimpleDateFormat("yyyy");

		return sf.format(new Date());
	}

	/**
	 * 获取当前时间的月
	 *
	 * @return
	 */
	public static String getMonth() {
		sf = new SimpleDateFormat("MM");

		return sf.format(new Date());
	}

	/**
	 * 获取当前时间的日
	 *
	 * @return
	 */
	public static String getDate() {
		sf = new SimpleDateFormat("dd");

		return sf.format(new Date());
	}


	/**
	 * 获得本周一0点时间
	 *
	 * @return
	 */
	public static Date getTimesWeekmorning() {
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return cal.getTime();
	}

	/**
	 * 获取当天0点是时间
	 *
	 * @return
	 */
	public static Date getTodayZeroTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);

		return calendar.getTime();
	}

	/**
	 * 获取当天晚上 11:59:59
	 *
	 * @return
	 */
	public static Date getTodayEndTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);

		return calendar.getTime();
	}


	/**
	 * 获取几个小时前的时间
	 *
	 * @param hoursBerore
	 * @return
	 */
	public static Date getHoursBeforeTime(int hoursBerore) {
		Calendar expireDate = Calendar.getInstance();
		expireDate.set(Calendar.HOUR_OF_DAY, expireDate.get(Calendar.HOUR_OF_DAY) - hoursBerore);
		return expireDate.getTime();
	}

	/**
	 * 获取几个小时后的时间
	 *
	 * @param hoursBerore
	 * @return
	 */
	public static Date getHoursAfterTime(int hoursAfter) {
		Calendar expireDate = Calendar.getInstance();
		expireDate.set(Calendar.HOUR_OF_DAY, expireDate.get(Calendar.HOUR_OF_DAY) + hoursAfter);
		return expireDate.getTime();
	}

	/**
	 * 获取几分钟前的时间
	 *
	 * @param minuteBerore
	 * @return
	 */
	public static Date getMinuteBeforeTime(int minuteBerore) {
		Calendar expireDate = Calendar.getInstance();
		expireDate.set(Calendar.MINUTE, expireDate.get(Calendar.MINUTE) - minuteBerore);
		return expireDate.getTime();
	}

	/**
	 * 获取几分钟后的时间
	 *
	 * @param minuteBerore
	 * @return
	 */
	public static Date getMinuteAfterTime(int minuteAfter) {
		Calendar expireDate = Calendar.getInstance();
		expireDate.set(Calendar.MINUTE, expireDate.get(Calendar.MINUTE) + minuteAfter);
		return expireDate.getTime();
	}

	/**
	 * 获取几天前的时间
	 *
	 * @param hoursBerore
	 * @return
	 */
	public static Date getDayBeforeTime(int dayBefore) {
		Calendar expireDate = Calendar.getInstance();
		expireDate.set(Calendar.DAY_OF_MONTH, expireDate.get(Calendar.DAY_OF_MONTH) - dayBefore);
		return expireDate.getTime();
	}

	/**
	 * 获取几天后的时间
	 *
	 * @param hoursBerore
	 * @return
	 */
	public static Date getDayAfterTime(Date date, int dayAfter) {
		Calendar expireDate = Calendar.getInstance();
		expireDate.setTime(date);
		expireDate.set(Calendar.DAY_OF_MONTH, expireDate.get(Calendar.DAY_OF_MONTH) + dayAfter);
		return expireDate.getTime();
	}

	/**
	 * 获取当月第一天时间
	 *
	 * @return
	 */
	public static Date getMonthStartTime() {
		// 获取当前年份、月份、日期
		Calendar cale = Calendar.getInstance();
		int year = cale.get(Calendar.YEAR);
		int month = cale.get(Calendar.MONTH) + 1;
		int day = cale.get(Calendar.DATE);
		int hour = cale.get(Calendar.HOUR_OF_DAY);
		int minute = cale.get(Calendar.MINUTE);
		int second = cale.get(Calendar.SECOND);
		int dow = cale.get(Calendar.DAY_OF_WEEK);
		int dom = cale.get(Calendar.DAY_OF_MONTH);
		int doy = cale.get(Calendar.DAY_OF_YEAR);

		// 获取前月的第一天
		cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		cale.set(Calendar.HOUR_OF_DAY, 0);
		cale.set(Calendar.MINUTE, 0);
		cale.set(Calendar.SECOND, 0);

		return cale.getTime();
	}

	/**
	 * 获取当月最后一天时间
	 *
	 * @return
	 */
	public static Date getMonthEndTime() {
		// 获取当前年份、月份、日期
		Calendar cale = Calendar.getInstance();
		int year = cale.get(Calendar.YEAR);
		int month = cale.get(Calendar.MONTH) + 1;
		int day = cale.get(Calendar.DATE);
		int hour = cale.get(Calendar.HOUR_OF_DAY);
		int minute = cale.get(Calendar.MINUTE);
		int second = cale.get(Calendar.SECOND);
		int dow = cale.get(Calendar.DAY_OF_WEEK);
		int dom = cale.get(Calendar.DAY_OF_MONTH);
		int doy = cale.get(Calendar.DAY_OF_YEAR);

		// 获取前月的最后一天
		cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 1);
		cale.set(Calendar.DAY_OF_MONTH, 0);

		return cale.getTime();
	}

}
